#include<iostream>
#include<cstring>
using namespace std;
const int N=3e6+10;
int get_num(char ch)
{
	if(ch >= 'a' && ch <= 'z') return ch - 'a';
	else if(ch >= 'A' && ch <= 'Z') return ch - 'A' + 26;
	else return ch - '0' + 52;
}
struct node
{
	int p,e;
	int flag[62];
}tree[N];
int idx=0;
void insert(string& s)
{
	int cur=0;
	tree[cur].p++;

	for(auto&e:s)
	{
		int path = get_num(e);
		if(tree[cur].flag[path]==0)
			tree[cur].flag[path]=++idx;
		cur=tree[cur].flag[path];
		tree[cur].p++;
	}

	tree[cur].e++;
}

int find_pre(string &s)
{
	int cur=0;
	for(auto&e:s)
	{
		int path = get_num(e);
		if(tree[cur].flag[path]==0)
		{
			return 0;
		}
		cur=tree[cur].flag[path];
	}
	return tree[cur].p;
}

int main()
{
	int t;cin>>t;

	while(t--)
	{
		int n,m;cin>>n>>m;
		memset(tree,0,sizeof(node)*(idx));
		idx=0;
		while(n--)
		{
			string s;cin>>s;
			insert(s);
		}

		while(m--)
		{
			string s;cin>>s;
			cout<<find_pre(s)<<endl;
		}
	}
}